<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 06/09/2017
 * Time: 01:05
 */
namespace frontend\controllers;

use common\models\ar\HistoryUserInfoAlter;
use common\models\ar\RealName;
use common\models\ar\UserInfoDetail;
use common\models\ar\UserInfoField;
use common\models\ar\UserInfoMenu;
use common\models\ar\UserTrade;
use common\models\Common;
use common\models\Sms;
use common\models\RealNameDao;
use common\models\UserDao;
use common\models\UserInfoDetailDao;
use yii\db\Query;
use common\models\ar\User;
class UserController extends FrontendController
{

    public function actionIndex(){
       $uid = \Yii::$app->user->identity->userID;
       $layout='//layouts/common-out';
        return $this->render('info_name', [
            'record' => RealName::findOne($uid),
            'fields' => UserInfoDetailDao::info($uid, 6, 1),
        ]);
    }
    /**
     * 账户实名
     */
    public function actionInfoRealName()
    {
        $uid = \Yii::$app->user->identity->userID;
        return $this->renderPartial('info-real-name', [
            'record' => RealName::findOne($uid),
            'fields' => UserInfoDetailDao::info($uid, 6, 1),
        ]);
    }

    /**
     * 提交实名验证
     */
    public function actionInfoRealNameSubmit()
    {
        $uid = \Yii::$app->user->identity->userID;
        $level_all=User::findOne(["userID"=>$uid]);
        //有末审核记录
        $tmp = RealName::findOne($uid);
        if ($tmp && $tmp->update_status == 1) {
            exit(json_encode(['code' => 0, 'msg' => '您有一条审核中的提交记录，请耐心等待']));
        }

        //所有实名字段
        $fields = [];
        $tmp = UserInfoField::find()
            ->where([
                'menu_id' => 6,//实名栏目
                'show_flag' => 1
            ])
            ->asArray()
            ->orderBy(['sort' => 'asc'])
            ->all();
        foreach ($tmp as $t) {
            $fields[$t['id']] = $t;
        }
        //已实名的字段信息
        $fieldsMy = [];
        $tmp = UserInfoDetail::find()->where(['userID' => $uid])->asArray()->all();
        foreach ($tmp as $t) {
            $fieldsMy[$t['field_id']] = $t;
        }

        $error = [];
        $updateFields = [];
        foreach ($fields as $k => $v) {
            if (!isset($fieldsMy[$k])) {
                //没有的字段
                if (!isset($_POST['f_' . $k])) {
                    //没有传递此字段
                    if ($v['is_must']) {
                        $error[] = $v['key'] . "为必填项";
                    } else {
                        $updateFields[$k] = '';
                    }
                } else {
                    //传递过来的值，要进行检测
                    $post = strip_tags($_POST['f_' . $k]);

                    $regExArr = explode('|', $v['regEx']);
                    if (isset($regExArr[0])) {
                        //有正则，按正则进行检测
                        $pattern = "/" . $regExArr[0] . "/";
                        if (!preg_match($pattern, $post)) {
                            $error[] = $v['key'] . "填写错误";
                        }
                    } else {
                        //没正则，查看是否是必填
                        if ($v['is_must'] && !$post) {
                            $error[] = $v['key'] . "为必填项";
                        }
                    }

                    $updateFields[$k] = $post;
                }
            } else {
                //已有的字段
                if (isset($_POST['f_' . $k])) {
                    if ($v['is_writable']) {
                        //可变更的字段
                        $post = strip_tags($_POST['f_' . $k]);

                        $regExArr = explode('|', $v['regEx']);
                        if (isset($regExArr[0])) {
                            //有正则，按正则进行检测
                            $pattern = "/" . $regExArr[0] . "/";
                            if (!preg_match($pattern, $post)) {
                                $error[] = $v['key'] . "填写错误";
                            }
                        } else {
                            //没正则，查看是否是必填
                            if ($v['is_must'] && !$post) {
                                $error[] = $v['key'] . "为必填项";
                            }
                        }

                        //内容是否变更
                        if ($post != $fieldsMy[$k]) {
                            $updateFields[$k] = $post;
                        }
                    }
                }
            }
        }
        if (!empty($error)) {
            exit(json_encode(['code' => 0, 'msg' => implode('<br>', $error)]));
        }
        if (empty($updateFields)) {
            exit(json_encode(['code' => 0, 'msg' => '资料末变更']));
        }
        $bool = RealNameDao::submit($uid, $fields, $updateFields, $level_all['accountName'],1,$level_all['loginName']);
        $users=User::findOne(['userID'=>$uid]);
        if($users->login_flag==2){
            $users->login_flag=3;
            $users->save(false);
        }
        if ($bool) {
            $code = 1;
            $msg = "已提交您的实名资料，请耐心等待审核结果";
        } else {
            $code = 0;
            $msg = "资料提交失败";
        }

        exit(json_encode(['code' => $code, 'msg' => $msg]));
    }

     //短信通知
    public function actionSendSms()
    {
        // echo "1";
        // exit();
        // $smsType = (int)\Yii::$app->request->get('smsType', 6);
        var_dump(\Yii::$app->request->get());
        exit();
        $return = Sms::send($cellphone, $smsType);
        exit(json_encode($return));
        }

    
}